package com.hengxinyongli.iam.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hengxinyongli.iam.entity.Role;
import com.hengxinyongli.iam.entity.RoleResource;
import com.hengxinyongli.iam.entity.UserRole;
import com.hengxinyongli.iam.mapper.RoleMapper;
import com.hengxinyongli.iam.mapper.RoleResourceMapper;
import com.hengxinyongli.iam.mapper.UserRoleMapper;
import com.hengxinyongli.iam.utils.AssertUtils;
import com.hengxinyongli.iam.web.constant.Res;
import com.hengxinyongli.iam.web.controller.RoleController;
import com.hengxinyongli.iam.entity.ResItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author magic
 * @version 1.0
 * Description RoleService
 * @date 2018/9/26/026 9:49
 */
@Service
public class RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleResourceMapper roleResourceMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    public PageInfo<Role> selectRoles(Integer pageNo, Integer pageSize, String roleName) {
        if (Objects.isNull(pageNo)) {
            pageNo = Integer.valueOf(1);
        }
        if (Objects.isNull(pageSize)) {
            pageSize = Integer.MAX_VALUE;
        }
        Role role = new Role();
        if (StringUtils.isNotEmpty(roleName)) {
            role.setName(roleName);
        }
        PageHelper.startPage(pageNo, pageSize);
        List<Role> roles = roleMapper.selectByModelSelective(role, false);
        Page<Role> page = ((Page) roles);
        return page.toPageInfo();
    }

    public Role selectByIdWithRes(String id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        AssertUtils.isTrue(Objects.nonNull(role), "指定角色ID不存在");
        List<ResItem> resources = roleResourceMapper.selectResByRoleId(id);
        role.setPermits(resources);
        return role;
    }

    public void roleCreate(Role role, Set<String> permits) {
        String rootParentId = role.getParentId();
        if (Res.ROOT_ID_LENGTH < role.getParentId().length()) {
            rootParentId = rootParentId.substring(0, 4);
        }
        String id = roleMapper.selectIncrementId(rootParentId);
        role.setId(id);
        roleMapper.insertSelective(role);

        permits.forEach(pid -> {
            RoleResource roleResource = new RoleResource();
            roleResource.setResourceId(pid);
            roleResource.setRoleId(role.getId());
            roleResourceMapper.insertSelective(roleResource);
        });
    }

    public void roleDelete(String id) {
        Role currentRole = roleMapper.selectByPrimaryKey(id);
        AssertUtils.isTrue(Objects.nonNull(currentRole), "指定角色ID不存在");
        UserRole userRole = new UserRole();
        userRole.setRoleId(id);
        List userRoles = userRoleMapper.selectByModelSelective(userRole,true);
        AssertUtils.isTrue(userRoles.isEmpty(), "指定角色ID绑定了用户信息!");
        roleMapper.deleteByPrimaryKey(id);
        RoleResource roleResource = new RoleResource();
        roleResource.setRoleId(id);
        roleResourceMapper.deleteByModelSelective(roleResource,true);
    }

    public void roleUpdate(String id, RoleController.RoleRo roleRo) {
        Role currentRole = roleMapper.selectByPrimaryKey(id);
        AssertUtils.isTrue(Objects.nonNull(currentRole), "指定角色ID不存在");
        Role updRole = new Role();
        updRole.setParentId(roleRo.getParentId());
        updRole.setDescription(roleRo.getDescription());
        updRole.setUpdateAt(new Date());
        updRole.setId(id);
        roleMapper.updateByPrimaryKeySelective(updRole);
        RoleResource roleResource = new RoleResource();
        roleResource.setRoleId(id);
        roleResourceMapper.deleteByModelSelective(roleResource, true);
        roleRo.getPermits().forEach(permit -> {
            RoleResource mapping = new RoleResource();
            mapping.setRoleId(id);
            mapping.setResourceId(permit);
            roleResourceMapper.insertSelective(mapping);
        });
    }


}
